/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons;

import org.dctmcommons.impl.VirtualDocHelper;
import org.junit.*;

import com.documentum.fc.client.IDfDocument;
import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfVirtualDocument;
import com.documentum.fc.client.IDfVirtualDocumentNode;
import com.documentum.fc.common.DfException;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import static com.documentum.fc.common.DfDocbaseConstants.*;

/**
 * Unit tests for {@link VirtualDocHelper}.
 * @author Mark Davidson
 */
public class VirtualDocHelperIT {
	
	/** Object name of root node doc. */
    private static final String ROOT_DOC = "rootDoc";

    /** Object name of child doc 1. */
	private static final String CHILD_DOC2 = "childDoc2";

	/** Object name of child doc 2. */
	private static final String CHILD_DOC1 = "childDoc1";

	/** The query helper. */
    private IVirtualDocHelper virtualDocHelper = null;
	
	/**
	 * Tests removal of test objects.
	 * @throws DfException The repository exception
	 */
	@Test
	public void testRemoveTestObjects() throws DfException {
		System.out.println(">>> testTestObjects");
		
		String testFolderPath = null;
		IDfFolder testFolder = null;
		IDfDocument virtualDoc = null;
		
		final IDfSession session = DctmCommonsTestUtil.getSession();
		
		session.beginTrans();
		try {
			testFolderPath = DctmCommonsTestUtil.createTestFolderPath();
			testFolder = session.getFolderByPath(testFolderPath);
			System.out.println("Got test folder path: " + testFolderPath);
			
			virtualDoc = createVirtualDoc(testFolder);
			
			session.commitTrans();
		} catch (DfException e) {
			session.abortTrans();
			fail("Failed to create test objects");
		}
		
		final String[] objectIds = virtualDocHelper.getChildDocumentIds(
											session,
											virtualDoc.getObjectId().getId(),
											false,
											false);
		assertThat("Null results array", objectIds, is(notNullValue()));
		assertThat("Incorrect child doc count", objectIds.length, is(2));

		DctmCommonsTestUtil.teardown(testFolderPath, true);
	}
	
	/**
	 * Creates some test objects.
	 * @param testFolder The test folder
	 * @return the virtual document
	 * @throws DfException The repository exception
	 */
	private IDfDocument createVirtualDoc(final IDfFolder testFolder)
		throws DfException {
		
		final IDfDocument childDoc1 = createDocument(
						CHILD_DOC1,
						testFolder.getObjectId().getId());
		final IDfDocument childDoc2 = createDocument(
						CHILD_DOC2,
						testFolder.getObjectId().getId());
		final IDfDocument rootNodeDoc = createDocument(
						ROOT_DOC,
						testFolder.getObjectId().getId());
		
		rootNodeDoc.checkout();
		rootNodeDoc.setIsVirtualDocument(true);
		final IDfVirtualDocument virtualDoc = rootNodeDoc.
							asVirtualDocument(null, false);
		final IDfVirtualDocumentNode rootNode = virtualDoc.getRootNode();
		
		virtualDoc.addNode(rootNode, null,
						   childDoc1.getChronicleId(),
						   null, false, false);
		
		virtualDoc.addNode(rootNode, null,
						   childDoc2.getChronicleId(),
						   null, false, false);
		
		rootNodeDoc.save();
		return rootNodeDoc;
	}

	/**
	 * Creates a document.
	 * @param docName The name of the document
	 * @param linkRef The link reference
	 * @return The document
	 * @throws DfException The repository exception
	 */
	private IDfDocument createDocument(final String docName,
									   final String linkRef)
		throws DfException {
		final IDfDocument doc =  (IDfDocument) DctmCommonsTestUtil
													.getSession()
														.newObject(
															DM_DOCUMENT);
		doc.setObjectName(docName);
		doc.link(linkRef);
		doc.save();
		return doc;
	}
	
	/**
	 * Setup routine.
	 */
	@Before
	public void before() {
		virtualDocHelper = VirtualDocHelper.getInstance();
	}
	
	/**
	 * Teardown routine.
	 */
	@After
	public void after() {
	}

}
